01. Introduction

C++ Optimization Techniques

Now comes the practical part of C++ optimization. You are going to learn a handful of code optimization strategies and then apply those strategies to increase the speed of a C++ program.

However, remember that optimizing a program involves other facets besides the programming language itself. Your program's speed will also depend on your hardware, your compiler and what computer algorithms you choose. The more familiar you become with all of these different facets, the more tools you will have for optimization. Here is a brief summary of why each of these aspects is so important.

Hardware

Some hardware might have limitations that slow down your code. For instance, when calculating trigonometric functions, a processor might use a slow software approximation. If instead, you could use small-angle approximation, you might get your code to run faster.

Embedded hardware might not have much memory or have a 16-bit or 32-bit architecture instead of a 64-bit architecture. Using 64-bit integers on a 16-bit architecture might be possible with your compiler, but it would also probably be inefficient.

Compilers

Many compilers will optimize at least parts of your code for you. For instance, it might be more efficient for the CPU to unroll a for loop to avoid checking the conditional statements:

// for loop
for (int i = 0; i < 5; i++) {
     std::cout << i << "\n";
}

// for loop unrolled
std::cout << 0 << "\n";
std::cout << 1 << "\n";
std::cout << 2 << "\n";
std::cout << 3 << "\n";
std::cout << 4 << "\n";

The unrolled version could run faster because unrolling avoids checking if i < 5 is true. For a more complete list of what your compiler might try to do, read this article here.

Algorithms

Some algorithms are known to be faster than other algorithms. A common case would be sorting algorithms; quicksort, for example, is known to be faster than bubble sort.

Here is another important point to keep in mind; C++ libraries are very convenient, but that doesn't mean they use the fastest algorithms especially for your individual case. Being aware of what is happening under the hood provides more opportunities for improving efficiency.

C++

And now, let's move on to optimizing C++. You are going to learn a handful of techniques and practice implementing them. Then at the end of the lesson, you'll have the opportunity to optimize a C++ histogram filter.

Here is a preview of the type of things you will be learning: Did you know that every time you call a function, C++ copies the input variables into memory? Take this example:

#include <iostream>

int addition(int a, int b);

int main() {

    int x, y;
    x = 5;
    y = 7;

    std::cout << addition(x, y) << "\n";

}

int addition(int a, int b) {
    return a + b;
}

C++ puts the x variable into memory and the y variable into memory as expected.

When you call the addition function, C++ actually then puts the a variable and the b variable into memory as well; essentially C++ is copying x and y into memory twice even though the x and y values could have been used directly.

For a 32-bit integer, this might not be an issue; however, once you start working with larger variables such as 2-D vectors, the extra read and writes can slow your programs down.

In this lesson, you'll learn how to speed up your code in situations like these.